<?php
class AlbumsCtl extends BaseCtl {
	public function AlbumsCtl(){
		parent::BaseCtl();
	}
    
	public function isPostback(){
		return 'albums' == $this->getPost('pagename');
	}

    public function loadData(){
    	$form = $this->getAlbumsForm();
    	
    	$form->setSearch($this->getPost('search'));
    	
    	$sel = $this->getPost('selected');
    	$arr = $this->getNonEmpty($sel);
    	$form->setSelected($arr);
    }
    
    public function edit(){
    	try {
    		$id = $this->getPost('idedit');
    		
    		$form = $this->getAlbumsForm();
    		$album = $form->findAlbum($id);
    		
    		$frm = $this->getEditAlbumForm();
    		$frm->clear();
    		
    		$frm->setAdding(false);
    		
    		$frm->setId($id);
    		$frm->setName($album->getName());
    		
    		$this->redirect('editalbum.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }

    public function add(){
    	try {
    		$form = $this->getEditAlbumForm(); 
    		$form->clear();
    		
    		$form->setAdding(true);
    		$service = new Service();
    		$form->setId($service->getNewId());
    		
    		$this->redirect('editalbum.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function delete(){
    	try {
			$form = $this->getAlbumsForm();
			$selected = $form->getSelected();
			
			if(count($selected) < 1){
				throw new Exception('Please select at least one album to delete');
			}
			else {
				$albums = $form->getSelectedAlbums($selected);
				$confirm = $this->getConfirmAlbumForm();
				$confirm->clear();
				
				$confirm->setAlbums($albums);
				
				$this->redirect('confirmalbum.php');
			}
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function search(){
    	try {
    		$form = $this->getAlbumsForm();
    		$search = $form->getSearch();
    		
    		$s = HtmlUtil::getSessionUser();
    		$userId = $s->getUser()->getId();
    		
    		$service = new Service();
    		$albums = $service->searchAlbums($userId, $search);
    		
    		$form->setAlbums($albums);
    		
    		if(count($albums) < 1){
    			HtmlUtil::addMessage('No albums found; please try again');
    		}
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
    public function select(){
    	try {
    		// get the id
    		$frm = $this->getAlbumsForm();
    		
    		$albumId = $this->getPost('idedit');
    		$album = $frm->findAlbum($albumId);
    		$albumName = $album->getName(); 
    		
    		// get the pictures for album
    		$service = new Service();
    		$pictures = $service->getPicturesByAlbumId($albumId);
    		
    		$form = $this->getPicturesForm();
    		$form->clear();
    		
    		$form->setPictures($pictures);
    		$form->setAlbumId($albumId);
    		$form->setAlbumName($albumName);
    		
    		$this->redirect('pictures.php');
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
    
} 
?>